/* This class was created to simplify form validation
   Written by Than Quang Minh.
   How to use:
   -  Create a CustomValidator Object;
   -  Add value and type of validation, error message.
   -  error return as an array of string.
    List of type checked by regular expression:
   required     :
   number       :
   digits       :
   email        :
   uscanzip     :
   usstate      :
   usphone      :
   creditcard   :
   ssn          :
   alpha        :
   alphanum     :
    List of type checked by Regular expression & code
   posint       : 
*/

var js_validators = 
{
    required: 
        {
            className: "required",
            defaultMessage: "This field is required",
            regExp: /\S/
        },    
    
    number: 
        {
            className: "number",
            defaultMessage: "This field must have a numerical value.",
            regExp: /^[-]?\d+(\.\d+)?$/
        },
        
    digits: 
    {
		className: "digits",
		defaultMessage: "This field can only contain numbers.",
		regExp: /^[-]?\d+(\.\d+)?$/
	},
    email: 
    {
		className: "email",
		defaultMessage: "This field must contain a valid email address.",
		regExp: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
	},
    uscanzip: 
    {
		className: "uscanzip",
		defaultMessage: "This field must contain a valid US or Canada zip code.",
		regExp: /^((\d{5}([- ])\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/
	},
    usstate:
    {
		className: "usstate",
		defaultMessage: "This field must contain a valid 2 letter US state code.",
		regExp: /^(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[ANU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$/
	},
    usphone: 
    {
		className: "usphone",
		defaultMessage: "This field must contain a valid US phone number with area code.",
		regExp: /^([0-9]( |-|.)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-|.)?([0-9]{3}( |-|.)?[0-9]{4}|[a-zA-Z0-9]{7})$/
	},
    creditcard: 
    {
		className: "creditcard",
		defaultMessage: "This field must contain a valid credit card number.",
		regExp: /^((4\d{3})|(5[1-5]\d{2})|(6011))([- ])?\d{4}([- ])?\d{4}([- ])?\d{4}|3[4,7]\d{13}$/
	},
    ssn: 
    {
		className: "ssn",
		defaultMessage: "This field must contain a valid social security number.",
		regExp: /(^|\s)(00[1-9]|0[1-9]0|0[1-9][1-9]|[1-6]\d{2}|7[0-6]\d|77[0-2])(-?|[\. ])([1-9]0|0[1-9]|[1-9][1-9])\3(\d{3}[1-9]|[1-9]\d{3}|\d[1-9]\d{2}|\d{2}[1-9]\d)($|\s|[;:,!\.\?])/
	},
    alpha: 
    {
		className: "alpha",
		defaultMessage: "This field must contain only letters.",
		regExp: /^[a-zA-z\s]+$/
	},
    alphanum: 
    {
		className: "alphanum",
		defaultMessage: "This field must contain only letters or numbers.",
		regExp: /^[a-zA-Z0-9]+$/
    },
    posint: 
    {
		className: "posint",
		defaultMessage: "This field must contain only positive integer.",
		regExp: /[0-9]/
    }
};


function ValidatorObj(variable_name, class_name, error_string)
{ 
    this.variable_name = variable_name;
    this.class_name = class_name;
    this.error_string = error_string; 
}
ValidatorObj.prototype = 
{
    constructor: ValidatorObj,
    sayError : 
        function () 
        {
            alert(this.error_string);
        },
    ValidateObject: 
        function ()
    	{
    		var bret = true;
    		for(items in js_validators)
	        {
		          if (this.class_name == js_validators[items]['className'])
		          {
        		              reg = js_validators[items]['regExp'];
        		              element = $(this.variable_name);
        		              if (!reg.test(element.value))
        		              {
        		                  bret = false;
        		                  if (this.error_string == undefined)
        		                  {
        		                      this.error_string = js_validators[items]['defaultMessage'];
        		                  }
        		              }
	              }
	        }
    		return bret;
    	}
}

function CustomValidator()
{
	this.validator_array = new Array();
    this.error_hash = new  Array();
	this.custom_validators= new Array();
}
CustomValidator.prototype =
{
    constructor: CustomValidator,
    AddValidator: 
        function(variable,validatorType,errorString)
        {
        	var validator_obj = new ValidatorObj(variable,validatorType,errorString);
        	this.validator_array.push(validator_obj);
        },

	DoValidate:
	    function()
    	{
    		var bret = true;    
            while (this.validator_array.length >0)
            {
                var validator = this.validator_array.shift();
                if (!validator.ValidateObject())
                {
    				bret = false;
                    this.error_hash.push(validator.error_string);
    			}    		    
            }
    		return bret;
    	}
}